In C language, strings are represented as arrays of characters, terminated by a null character ('\0'). The C Standard Library provides several string manipulation functions to work with strings. Here, I'll explain some commonly used string library functions along with sample code, programming comments, and sample output.
Name | Description |
strlen() | Returns the length of the string |
strcpy() | Copy one string to another |
strncpy() | Copy first n characters of one string to another |
strcat() | Concatenates two strings |
strncat() | Concatenates first n characters of one string to another |
strcmp() | Compares two strings |
strncmp() | Compares first n characters of two strings |
strchr() | Find the first occurrence of the given character in the string |
strrchr() | Finds the last occurrence of the given characters in the string |
strstr() | Find the given substring in the string |
strcspn() | Returns the span of the source string not containing any character of the given string |
strspn() | Returns the span of the source string containing only the characters of the given string |
strpbrk() | Finds the first occurrence of any of the characters of the given string in the source string |
strtok() | Split the given string into tokens based on some character as a delimiter |
strcoll() | Compares two strings that are passed |
memset() | Initialize a block of memory with the given character |
memcmp() | Compares two blocks of memory |
memcpy() | Copy two blocks of memory |
memmove() | Moves two blocks of memory |
memchr() | Finds the given character in the block of memory |
Explanation:The strlen function is used to calculate the length of a given string (the number of characters before the null terminator).
Usage:size_t strlen(const char* str);
#include <stdio.h>
#include <string.h>
int main() {
char message[] = "Hello, World!";
size_t length = strlen(message);
printf("Length of the string: %zu\n", length);
return 0;
}
Length of the string: 13
Explanation:The strcpy function is used to copy the contents of one string to another.
Usage:char* strcpy(char* dest, const char* src);
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
Copied string: Hello, World!
Explanation:The strcmp function is used to compare two strings lexicographically.
Usage:int strcmp(const char* str1, const char* str2);
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
if (result < 0) {
printf("%s comes before %s\n", str1, str2);
} else if (result > 0) {
printf("%s comes after %s\n", str1, str2);
} else {
printf("%s is equal to %s\n", str1, str2);
}
return 0;
}
apple comes before banana
Explanation:The strcat function is used to concatenate (append) one string to the end of another string.
Usage:char* strcat(char* dest, const char* src);
#include <stdio.h>
#include <string.h>
int main() {
char str1[30] = "Hello";
char str2[] = ", World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Concatenated string: Hello, World!
Explanation:The strchr function is used to find the first occurrence of a specific character in a string.
Usage:char* strchr(const char* str, int character);
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char character = 'W';
char* result = strchr(str, character);
if (result != NULL) {
printf("Character '%c' found at position: %ld\n", character, result - str);
} else {
printf("Character '%c' not found.\n", character);
}
return 0;
}
Character 'W' found at position: 7
Explanation:The strstr function is used to find the first occurrence of a substring in a string.
Usage:char* strstr(const char* str, const char* substr);
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
char* result = strstr(str, substr);
if (result != NULL) {
printf("Substring '%s' found at position: %ld\n", substr, result - str);
} else {
printf("Substring '%s' not found.\n", substr);
}
return 0;
}
Substring 'World' found at position: 7
These are some commonly used string library functions in C, along with sample code, programming comments, and sample outputs. They are essential for various string manipulation tasks in C programming.
What C library function is used to find the length of a string?
Which function is used to concatenate two strings in C?
What C function is used to copy one string to another?
Which function is used to compare two strings in C for equality?
What function in C is used to find the first occurrence of a substring in a string?